package com.rongke.web.controller.medical;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import jodd.util.StringUtil;

import com.alibaba.fastjson.JSONObject;
import com.rongke.commons.JsonResp;
import com.rongke.model.InspectionRecord;
import com.rongke.model.dict.DictInspection;
import com.rongke.service.DictInspectionService;
import com.rongke.service.InspectionRecordService;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

/**
 * 检查记录
 * 
 * @version : Ver 1.0
 */
@RestController
@RequestMapping(value = "/api/inspectionRecord")
@Transactional
public class InspectionRecordController {
	private static final Logger LOGGER = Logger
			.getLogger(InspectionRecordController.class);

	@Autowired
	private InspectionRecordService inspectionRecordService;

	@Autowired
	private DictInspectionService dictInspectionService;

	/**
	 * @添加检查记录
	 * @param inspectionRecord
	 * @return 返回值JsonResp
	 */
	@RequestMapping(value = "/add", method = RequestMethod.POST)
	public JsonResp addInspectionRecord(
			@RequestBody InspectionRecord inspectionRecord) {
		LOGGER.debug("添加检查记录");

		try {
			// fillIdWithName(inspectionRecord);
			inspectionRecordService.insert(inspectionRecord);
			JSONObject json = new JSONObject();
			json.put("id", inspectionRecord.getId());
			return JsonResp.ok(json);
		} catch (Exception e) {
			LOGGER.error("addInspectionRecord failed", e);
			return JsonResp.fa(null);
		}
	}

	/**
	 * @修改检查记录
	 * @param inspectionRecord
	 * @return 返回值JsonResp
	 */
	@RequestMapping(value = "/update", method = RequestMethod.POST)
	public JsonResp updateInspectionRecord(
			@RequestBody InspectionRecord inspectionRecord) {
		LOGGER.debug("updateInspectionRecord");

		try {
			inspectionRecord.setGmtCreate(null);// 不修改创建时间
			inspectionRecord.setGmtModified(new Date());
			inspectionRecord.setPicContent(null);
			inspectionRecord.setUserId(null);
			inspectionRecord.setMedicalRecordId(null);

			// fillIdWithName(inspectionRecord);
			inspectionRecordService.updateById(inspectionRecord);

			JSONObject json = new JSONObject();
			json.put("id", inspectionRecord.getId());
			return JsonResp.ok(json);
		} catch (Exception e) {
			LOGGER.error("updateInspectionRecord failed", e);
			return JsonResp.fa(null);

		}
	}

	/**
	 * 根据ID删除检查记录
	 * 
	 * @param id
	 * @return
	 */
	@RequestMapping(value = "/del", method = RequestMethod.POST)
	public JsonResp delInspectionRecordById(long id) {
		if (LOGGER.isInfoEnabled()) {
			LOGGER.info("delInspectionRecordById, id=" + id);
		}

		try {
			inspectionRecordService.deleteById(id);

			JSONObject json = new JSONObject();
			json.put("id", id);
			return JsonResp.ok(json);
		} catch (Exception e) {
			LOGGER.error("delInspectionRecordById failed, id=" + id, e);
			return JsonResp.fa(null);
		}
	}

	/**
	 * @根据id查找检查记录
	 * @param id
	 * @return 返回值JsonResp
	 */
	@RequestMapping(value = "/selectOne", method = RequestMethod.GET)
	public JsonResp selectInspectionRecord(Long id) {
		LOGGER.debug("查找检查记录");
		InspectionRecord inspectionRecord = inspectionRecordService
				.selectById(id);
		// fillWithCheckTypeName(inspectionRecord);
		return JsonResp.ok(inspectionRecord);
	}

	// private void fillWithCheckTypeName(InspectionRecord inspectionRecord) {
	// if (null == inspectionRecord) {
	// return;
	// }
	//
	// List<DictInspection> dictList = dictInspectionService.selectByMap(null);
	// for (DictInspection dictInspection : dictList) {
	// if (inspectionRecord.getCheckType().equals(
	// String.valueOf(dictInspection.getId()))) {
	// inspectionRecord.setCheckTypeName(dictInspection.getName());
	// return;
	// }
	// }
	// }

	@RequestMapping(value = "/selectAll", method = RequestMethod.GET)
	public JsonResp selectList(long medicalRecordId) {
		if (LOGGER.isInfoEnabled()) {
			LOGGER.info("selectList, medicalRecordId=" + medicalRecordId);
		}

		try {
			Map<String, Object> params = new HashMap<String, Object>();
			params.put("medical_record_id", medicalRecordId);
			List<InspectionRecord> result = inspectionRecordService
					.selectByMap(params);
			for (InspectionRecord record : result) {
				// fillWithCheckTypeName(record);
			}
			return JsonResp.ok(result);
		} catch (Exception e) {
			LOGGER.error("selectList failed, medicalRecordId="
					+ medicalRecordId, e);
			return JsonResp.fa(null);
		}
	}

	// private void fillIdWithName(InspectionRecord record) {
	// if (null == record) {
	// return;
	// }
	//
	// if (StringUtil.isNotEmpty(record.getCheckType())) {
	// return;
	// }
	//
	// List<DictInspection> dictList = dictInspectionService.selectByMap(null);
	// for (DictInspection dict : dictList) {
	// if (record.getCheckTypeName().equals(dict.getName())) {
	// record.setCheckType(String.valueOf(dict.getId()));
	// return;
	// }
	// }
	// }
}
